﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DataAccessLayer.DataInterfaces;
using MyCommon;

namespace DataAccessLayer
{
    public class PhienDAO : IPhien
    {
        private static QLBaoHanh dataContext;
        private IRepository<Phien> repository;
        public PhienDAO()
        {
            dataContext = new QLBaoHanh();
            repository = new RepositoryDAO<Phien>(dataContext);
        }

        public IEnumerable<Phien> GetAllPhien()
        {
            return repository.GetBy(aobj => aobj.isDelete == false);
        }
        public Phien GetPhienById(int Id)
        {
            return repository.Find(obj => obj.Id == Id).Single();
        }
        public Phien GetLastRecord()
        {
            return dataContext.Phiens.OrderByDescending(obj => obj.Id).FirstOrDefault();
        }

        public bool UpdatePhien(Phien inPhien)
        {
            if (inPhien == null) return false;
            Phien upPhien = repository.First(sobj => sobj.Id == inPhien.Id);
            if (upPhien == null) return false;
            upPhien = inPhien;
            return repository.SaveChanges() > 0;
        }
        public bool InsertPhien(Phien inPhien)
        {
            if (inPhien == null) return false;
            inPhien.isDelete = false;
            repository.Add(inPhien);
            return repository.SaveChanges() > 0;
        }

        public ReturnValue<bool> DelPhien(int Id)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                Phien objDel = repository.First(obj => obj.Id == Id);
                ret = DelPhien(objDel);
            }
            catch (Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }


        public ReturnValue<bool> DelPhien(Phien inPhien)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inPhien == null)
            {
                ret.Msg = "input null";
                return ret;
            }
            try
            {
                repository.Delete(inPhien);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }
        
        public IEnumerable<View_Phien_ThietBiBH> GetThongTinThietBiByPhienId(int PhienId)
        {
            var q = from e in dataContext.View_Phien_ThietBiBH
                where e.PhienId == PhienId
                select e;
            return q.AsEnumerable<View_Phien_ThietBiBH>();
        }
        public IEnumerable<View_Phien_NhanVienThucHien> GetNhanVienThucHienByPhienId(int inPhienId)
        {
            var q = from e in dataContext.View_Phien_NhanVienThucHien
                    where e.PhienId == inPhienId
                    select e;
            return q.AsEnumerable<View_Phien_NhanVienThucHien>();
        }
        
        public IEnumerable<Phien> GetPhienBetween(DateTime inFromDate, DateTime inToDate)
        {
            DateTime FromDate = new DateTime(inFromDate.Year, inFromDate.Month, inFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inToDate.Year, inToDate.Month, inToDate.Day, 23, 59, 59);
            return repository.GetBy(aobj => aobj.ThoiGianNhan >= FromDate && aobj.ThoiGianNhan <= ToDate && (aobj.TrangThaiPhienId == (int)MyCommon.TrangThaiPhien.ThaoTacNgay || aobj.TrangThaiPhienId == (int)MyCommon.TrangThaiPhien.ThaoTacNgay_HoanThanh));
        }
        public IEnumerable<Phien> GetPhienBetween(DateTime inFromDate, DateTime inToDate, bool inOnlyChuaHoanThanh)
        {
            DateTime FromDate = new DateTime(inFromDate.Year, inFromDate.Month, inFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inToDate.Year, inToDate.Month, inToDate.Day, 23, 59, 59);
            return repository.GetBy(aobj => aobj.ThoiGianNhan >= FromDate && aobj.ThoiGianNhan <= ToDate && aobj.TrangThaiPhienId == (int)MyCommon.TrangThaiPhien.ThaoTacNgay);
        }


        public IEnumerable<Phien> GetPhienTiepNhanBetween(DateTime inFromDate, DateTime inToDate)
        {
            DateTime FromDate = new DateTime(inFromDate.Year, inFromDate.Month, inFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inToDate.Year, inToDate.Month, inToDate.Day, 23, 59, 59);
            return repository.GetBy(aobj => aobj.ThoiGianNhan >= FromDate && aobj.ThoiGianNhan <= ToDate && (aobj.TrangThaiPhienId == (int)MyCommon.TrangThaiPhien.TiepNhan_DoiBaoHanh || aobj.TrangThaiPhienId == (int)MyCommon.TrangThaiPhien.DoiBaoHanh_HoanThanh));
        }
        public IEnumerable<Phien> GetPhienTiepNhanBetween(DateTime inFromDate, DateTime inToDate, bool inOnlyChuaHoanThanh)
        {
            DateTime FromDate = new DateTime(inFromDate.Year, inFromDate.Month, inFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inToDate.Year, inToDate.Month, inToDate.Day, 23, 59, 59);
            return repository.GetBy(aobj => aobj.ThoiGianNhan >= FromDate && aobj.ThoiGianNhan <= ToDate && aobj.TrangThaiPhienId == (int)MyCommon.TrangThaiPhien.TiepNhan_DoiBaoHanh);
        }


        public bool IsPhienComplete(int inPhienId)
        {
            var data = from e in dataContext.Phien_ThietBiBH
                           where e.PhienId == inPhienId && e.TrangThaiThietBiBHID != (int)MyCommon.TrangThaiThietBiBH.DaTra
                           select e;
            if (data.Count() > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        

    }
}